package com.colin.app.activity.view.listview;

import android.animation.ObjectAnimator;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewConfiguration;
import android.widget.AbsListView;
import android.widget.TextView;

import com.colin.app.R;
import com.colin.app.activity.view.ViewContract;
import com.colin.app.activity.view.ViewPresenterImpl;
import com.colin.app.adapter.ListAdapter;
import com.colin.app.app.AppActivity;
import com.colin.app.bean.DemoBean;
import com.colin.library.view.MyListView;

public class ListViewActivity extends AppActivity implements ViewContract.ViewView {
    private ViewContract.ViewPresenter mViewPresenter = null;
    private MyListView list_example_show;
    private TextView text_list_empty;
    private ListAdapter adapter = null;
    private int mTouchSlop = 0;

    @Override
    protected void onDestroy() {
        if (null!=mViewPresenter){
            mViewPresenter.destroy();
            mViewPresenter = null;
        }
        super.onDestroy();
    }

    @Override
    public void initPresenter() {
        DemoBean demoBean = getIntent().getExtras().getParcelable("clazz");
        mViewPresenter = new ViewPresenterImpl(this, this, demoBean);
    }


    @Override
    public int initViewLayoutID() {
        return R.layout.activity_list_view;
    }

    @Override
    public void initView() {
        this.list_example_show = (MyListView) this.findViewById(R.id.list_example_show);
        this.text_list_empty = (TextView) this.findViewById(R.id.text_list_empty);
        initListView();
    }

    private void initListView() {
        list_example_show.setEmptyView(text_list_empty);

        View header = new View(this);
        header.setLayoutParams(new AbsListView.LayoutParams(AbsListView.LayoutParams.MATCH_PARENT, (int) getResources().getDimension(
                R.dimen.abc_action_bar_default_height_material)));
        list_example_show.addHeaderView(header);

        if (null == adapter) {
            adapter = new ListAdapter(this, mViewPresenter.listGetItemData(true));
        }
        list_example_show.setAdapter(adapter);
        list_example_show.setOnTouchListener(myTouchListener);

    }

    @Override
    public void initData() {
        //最低滑动距离
        mTouchSlop = ViewConfiguration.get(this).getScaledTouchSlop();

        mViewPresenter.setData();
    }

    private int lastVisibleItemPosition = 0;
    //设置 滑动隐藏ToolBar 添加动画效果
    private float mFirstY = 0f;
    private float mCurrentY = 0f;
    private int direction = 0;
    private boolean mShowToolBar = true;
    private ObjectAnimator mAnimator;

    @Override
    public void initListener() {

//        this.list_example_show.setOnTouchListener(new View.OnTouchListener() {
//            @Override
//            public boolean onTouch(View v, MotionEvent event) {
//                switch (event.getAction()) {
//                    case MotionEvent.ACTION_DOWN://触碰时操作
//                        mFirstY = event.getY();
//                        break;
//                    case MotionEvent.ACTION_MOVE://移动时操作
//                        mCurrentY = event.getY();
//                        if (mFirstY - mCurrentY < mTouchSlop) {
//                            direction = 0;//down
//                        } else if (mFirstY - mCurrentY > mTouchSlop) {
//                            direction = 1;//up
//                        }
//                        LogHelp.d("mFirstY==" + String.valueOf(mFirstY));
//                        LogHelp.d("mCurrentY==" + String.valueOf(mCurrentY));
//                        LogHelp.d("direction==" + String.valueOf(direction));
//                        LogHelp.d("mShowToolBar==" + String.valueOf(mShowToolBar));
//
//                        if (direction == 1 && mShowToolBar) {
//                            toolBarAnim(0);//hide
//                            mShowToolBar = !mShowToolBar;
//                        } else if (direction == 0 && !mShowToolBar) {
//                            toolBarAnim(1);//hide
//                            mShowToolBar = !mShowToolBar;
//                        }
//
//                        break;
//                    case MotionEvent.ACTION_UP://离开时操作
//                        break;
//                }
//                return false;
//            }
//        });

        this.list_example_show.setOnScrollListener(new AbsListView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(AbsListView view, int scrollState) {
                switch (scrollState) {
                    case AbsListView.OnScrollListener.SCROLL_STATE_IDLE://滑动停止
                        break;
                    case AbsListView.OnScrollListener.SCROLL_STATE_TOUCH_SCROLL://正在滚动
                        break;
                    case AbsListView.OnScrollListener.SCROLL_STATE_FLING://手指有抛动，惯性滑动
                        break;
                }
            }

            /**
             *
             * @param view
             * @param firstVisibleItem
             * @param visibleItemCount
             * @param totalItemCount
             */
            @Override
            public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
                lastVisibleItemPosition = firstVisibleItem;
                //滑动的时候一直调用
                if (firstVisibleItem + visibleItemCount == totalItemCount && totalItemCount > 0) {
                    //滚动到最后一行
                }

                if (firstVisibleItem > lastVisibleItemPosition) {//上滑

                } else if (firstVisibleItem < lastVisibleItemPosition) {//下滑

                }

            }
        });
    }

    View.OnTouchListener myTouchListener = new View.OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            switch (event.getAction()) {
                case MotionEvent.ACTION_DOWN:
                    mFirstY = event.getY();
                    break;
                case MotionEvent.ACTION_MOVE:
                    mCurrentY = event.getY();
                    if (mCurrentY - mFirstY > mTouchSlop) {
                        direction = 0;// down
                    } else if (mFirstY - mCurrentY > mTouchSlop) {
                        direction = 1;// up
                    }
                    if (direction == 1) {
                        if (mShowToolBar) {
                            toolBarAnim(1);//show
                            mShowToolBar = !mShowToolBar;
                        }
                    } else if (direction == 0) {
                        if (!mShowToolBar) {
                            toolBarAnim(0);//hide
                            mShowToolBar = !mShowToolBar;
                        }
                    }
                    break;
                case MotionEvent.ACTION_UP:
                    break;
            }
            return false;
        }
    };

    /**
     * 隐藏ToolBar 动画
     *
     * @param flag
     */
    private void toolBarAnim(int flag) {
        if (null != mAnimator && mAnimator.isRunning()) {
            mAnimator.cancel();
        }
        if (flag == 0) {
            mAnimator = ObjectAnimator.ofFloat(toolbar, "translationY", toolbar.getTranslationY(), 0);
        } else {
            mAnimator = ObjectAnimator.ofFloat(toolbar, "translationY", toolbar.getTranslationY(), -100);
        }
        mAnimator.start();
    }
}
